package com.zbkj.admin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.zbkj.admin.service.IAdminFileHandleService;
import com.zbkj.common.exception.CommonException;
import com.zbkj.common.model.file.PeFile;
import com.zbkj.common.model.file.PeFileType;
import com.zbkj.common.request.file.AdminFileSaveRequest;
import com.zbkj.common.request.file.AdminFileUpdateRequest;
import com.zbkj.common.response.file.AdminFileResponse;
import com.zbkj.common.response.file.AdminFileTypeResponse;
import com.zbkj.service.service.file.IPeFileService;
import com.zbkj.service.service.file.IPeFileTypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AdminFileHandleServiceImpl implements IAdminFileHandleService {

    @Autowired
    private IPeFileService peFileService;

    @Autowired
    private IPeFileTypeService peFileTypeService;

    @Override
    public void adminFileSave(AdminFileSaveRequest adminFileSaveRequest) {
        if(adminFileSaveRequest.getFileFlag() == 1 && ObjectUtil.isEmpty(adminFileSaveRequest.getFileUrl())){
            throw new CommonException("请上传对应的文件路径");
        }
        if(adminFileSaveRequest.getFileFlag() == 2 && ObjectUtil.isEmpty(adminFileSaveRequest.getFileContent())){
            throw new CommonException("请上传对应的内容");
        }
        PeFile peFile = new PeFile();
        BeanUtils.copyProperties(adminFileSaveRequest,peFile);
        peFile.setDeleteFlag(0);
        peFileService.save(peFile);
    }

    @Override
    public boolean adminFileTypeSave(String fileTypeName) {
        Integer count = peFileTypeService.lambdaQuery().eq(PeFileType::getFileTypeName, fileTypeName)
                .eq(PeFileType::getDeleteFlag, 0).count();
        if(count > 0){
            throw new CommonException("当前文件类型已存在");
        }
        PeFileType peFileType = new PeFileType();
        peFileType.setFileTypeName(fileTypeName);
        return peFileTypeService.save(peFileType);
    }

    @Transactional
    @Override
    public boolean adminFileTypeDel(Long fileTypeId) {
        PeFileType peFileType = peFileTypeService.getById(fileTypeId);
        if(ObjectUtil.isEmpty(peFileType)){
            throw new CommonException("当前文件类型不存在");
        }
        peFileType.setDeleteFlag(1);
        peFileTypeService.updateById(peFileType);
        peFileService.lambdaUpdate().eq(PeFile::getFileType, peFileType.getId())
                .set(PeFile::getDeleteFlag, 1);
        return true;
    }

    @Override
    public List<AdminFileTypeResponse> adminFileTypeList() {
        List<AdminFileTypeResponse> returnList = new ArrayList<>();
        List<PeFileType> list = peFileTypeService.lambdaQuery().eq(PeFileType::getDeleteFlag, 0).list();
        if(ObjectUtil.isEmpty(list)){
            return returnList;
        }

        List<Long> fileTypeIds = list.stream().map(PeFileType::getId).collect(Collectors.toList());
        List<PeFile> files = peFileService.lambdaQuery().in(PeFile::getFileType, fileTypeIds)
                .eq(PeFile::getDeleteFlag, 0).list();
        List<AdminFileResponse> fileResponsesList = new ArrayList<>();
        Map<Long, List<AdminFileResponse>> fileTypeGroup = new HashMap<>();
        if(ObjectUtil.isNotEmpty(files)){
            for (PeFile file : files) {
                AdminFileResponse adminFileResponse = new AdminFileResponse();
                BeanUtils.copyProperties(file,adminFileResponse);
                fileResponsesList.add(adminFileResponse);
            }
            fileTypeGroup = fileResponsesList.stream().collect(Collectors.groupingBy(AdminFileResponse::getFileType));
        }

        for (PeFileType peFileType : list) {
            AdminFileTypeResponse adminFileTypeResponse = new AdminFileTypeResponse();
            BeanUtils.copyProperties(peFileType,adminFileTypeResponse);
            if(fileTypeGroup.containsKey(peFileType.getId())){
                adminFileTypeResponse.setFileList(fileTypeGroup.get(peFileType.getId()));
            }
            returnList.add(adminFileTypeResponse);
        }
        return returnList;
    }

    @Override
    public boolean adminFileDel(Long fileId) {
        PeFile peFile = peFileService.getById(fileId);
        if(ObjectUtil.isEmpty(peFile)){
            throw new CommonException("当前文件不存在");
        }
        peFile.setDeleteFlag(1);
        return peFileService.updateById(peFile);
    }

    @Override
    public boolean adminFileUpdate(AdminFileUpdateRequest adminFileUpdateRequest) {
        if(adminFileUpdateRequest.getFileFlag() == 1 && ObjectUtil.isEmpty(adminFileUpdateRequest.getFileUrl())){
            throw new CommonException("请上传对应的文件路径");
        }
        if(adminFileUpdateRequest.getFileFlag() == 2 && ObjectUtil.isEmpty(adminFileUpdateRequest.getFileContent())){
            throw new CommonException("请上传对应的内容");
        }
        PeFile peFile = new PeFile();
        BeanUtils.copyProperties(adminFileUpdateRequest,peFile);
        return peFileService.updateById(peFile);
    }
}
